<template>
    <div class="e-form">
        <BillTop  @cancel="handleClose"></BillTop>
        <div class="tabs">
            <el-tabs
                tab-position="left"
                v-model="tabsName"
                @tab-click="onChangeTab"
            >
                <el-tab-pane
                    label="基本信息"
                    name="baseInfo"
                    :disabled="clickTabFlag"
                ></el-tab-pane>
                <el-tab-pane
                    label="调拨明细"
                    name="detailed"
                    :disabled="clickTabFlag"
                ></el-tab-pane>
                <el-tab-pane
                    label="附件信息"
                    name="attach"
                    :disabled="clickTabFlag"
                ></el-tab-pane>
  <el-tab-pane
                    label="审核历史"
                    name="auditHistory"
                    :disabled="clickTabFlag"
                ></el-tab-pane>
                <div>
                    <div id="baseInfoCon" class="con">

                        <div class="tabs-title" id="baseInfo">基本信息</div>
                        <div style="width: 100%" class="form">
                                   <el-form :model="basicForm" :label-width="formLabelWidth">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="消耗分摊编号：">
                                    <span>{{ basicForm.billNo }}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                               <el-form-item label="机构：">
                                    <span>{{ basicForm.orgName }}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="分包商：">
                                    <span>
                                        {{
                                            basicForm.subcontractorName
                                        }}
                                    </span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                              <el-form-item label="消耗日期：">
                                    <span>
                                        {{
                                            basicForm.apportionedDate
                                        }}
                                    </span>
                                </el-form-item>
                            </el-col>
                        </el-row>
  <el-row>
                            <el-col :span="12">
                                <el-form-item label="业务类型：">
                                    <span>{{ basicForm.businessTypeKey }}</span>
                                </el-form-item>
                            </el-col>

                        </el-row>
                        <el-row>

                             <el-col :span="12">

                                    <el-form-item label="状态：">
                                 <span>
                                       {{stateArr[basicForm.state]}}
                                   </span>
                                </el-form-item>
                            </el-col>
  <el-col :span="12">
                                <el-form-item label="录入人：">
                                 <span>{{
                                     basicForm.inputName
                                     }}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>

                           <el-row>
                            <el-col :span="24">
                                <el-form-item label="备注：">
                                      <span>{{
                                     basicForm.remarks
                                     }}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>

                    </el-form>
                        </div>
                    </div>
                    <!-- 物资明细 -->
                    <div id="detailedCon" class="con">
                        <div class="tabs-title" id="detailed">闲置明细</div>
                        <div class="e-table">
                            <div class="top">
                                <div class="left">
                                    <el-button size="mini" type="primary"
                                        >基础库引入</el-button
                                    >
                                    <el-button size="mini">删除</el-button>
                                </div>
                            </div>
                            <el-table
                                :data="detailedTableData"
                                border
                                 :max-height="$store.state.tableHeight"
                                style="width: 100%"
                                ref="table"
                                 v-sortTable="{ 'tableData': detailedTableData, '_this': this, 'ref': 'table' }"
                            >
                                <el-table-column type="selection" width="55">
                                </el-table-column>
                                <el-table-column
                                    prop="a1"
                                    label="类别"
                                    width="200"
                                >
                                </el-table-column>
                                <el-table-column
                                    prop="a"
                                    label="物资名称"
                                    width="200"
                                >
                                </el-table-column>
                                <el-table-column
                                    prop="b"
                                    label="规格"
                                    width="200"
                                >
                                </el-table-column>
                                <el-table-column
                                    prop="c"
                                    label="计量单位"
                                    width="200"
                                >
                                </el-table-column>
                                <el-table-column
                                    prop="c"
                                    label="材质"
                                    width="200"
                                >
                                </el-table-column>
                                <el-table-column
                                    prop="c"
                                    label="需求数量"
                                    width="200"
                                >
                                </el-table-column>
                                <el-table-column
                                    prop="c"
                                    label="可调配数"
                                    width="200"
                                >
                                </el-table-column>
                                <el-table-column
                                    prop="c"
                                    label="建议租赁数"
                                    width="200"
                                >
                                </el-table-column>
                                <el-table-column
                                    prop="c"
                                    label="建议采购数"
                                    width="200"
                                >
                                </el-table-column>
                                <el-table-column
                                    prop="c"
                                    label="单价(元)"
                                    width="200"
                                >
                                </el-table-column>
                                <el-table-column
                                    prop="c"
                                    label="金额(元)"
                                    width="200"
                                >
                                </el-table-column>
                                <el-table-column
                                    prop="c"
                                    label="开始使用时间"
                                    width="200"
                                >
                                </el-table-column>
                                <el-table-column
                                    prop="c"
                                    label="结束使用时间"
                                    width="200"
                                >
                                </el-table-column>
                            </el-table>
                            <ComPagination />
                        </div>
                    </div>
                    <!-- 附件信息 -->
                    <div id="attachCon" class="con">
                        <div class="tabs-title" id="attach">照片信息</div>
<attach-comp :relationId="attachId" bucketName="turnover" isView/>

                    </div>
                       <!-- 审核历史 -->
                    <div id="auditHistoryCon" class="con">
                        <div class="tabs-title" id="auditHistory">审核历史</div>
                        <div class="e-table">
                            <ComAuditHistoryIndex
                                billType="6083"
                                ref="AuditHistoryRef"
                            />
                        </div>
                    </div>
                </div>
            </el-tabs>
        </div>
        <div class="buttons">
            <el-button type="primary" size="small" style="background: #2e61d7"
                >打印</el-button
            >
            <el-button type="primary" size="small" style="background: #2e61d7"
                >审核</el-button
            >

            <el-button
                type="primary"
                size="small"
                style="background: #2e61d7"
                @click="onSave"
                >保存</el-button
            >
            <el-button type="primary" size="small" style="background: #2e61d7"
                >保存并提交</el-button
            >
            <el-button type="danger" size="small">撤回</el-button>
            <el-button type="danger" size="small" @click="clickDel">删除</el-button>
            <el-button size="small" @click="handleClose">关闭</el-button>
        </div>
        <div id="seat" :style="{ height: seatHeight + 'px' }"></div>
    </div>
</template>

<script>
import ComPagination from '@/components/pagination/pagination.vue'
import AttachComp from '@/components/common/attachIndex'
import ComAuditHistoryIndex from '@/pages/revolveMaterialManage/components/auditHistory/auditHistoryIndex'

import $ from 'jquery'
import '@/utils/jquery.scrollTo.min'
export default {
    components: {
        ComPagination, AttachComp, ComAuditHistoryIndex
    },
    computed: {
        // 填补底部空白，以使高度够滚动
        seatHeight () {
            return this.screenHeight - 72 - this.lastConHeight
        },
        attachId () {
            return this.id
        }
    },
    mounted () {
        this.getLastConHeight()
        // 保存所有tabName
        const arr = ['baseInfo', 'detailed', 'attach', 'auditHistory']

        let $idsTop = []
        window.addEventListener('scroll', () => {
            // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            if (this.clickTabFlag) {
                return
            }

            if (!$idsTop[$idsTop.length - 1]) {
                $idsTop = arr.map(item => {
                    const itemTop = document.getElementById(item).offsetTop
                    return itemTop
                })
            }
            const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
            // 倒序查找
            let curLocal = 0
            for (let len = $idsTop.length, i = len - 1; i >= 0; i--) {
                let item = $idsTop[i]
                if ((scrollTop + 1) >= item) {
                    curLocal = i
                    break
                }
            }
            // 设置对应tabName
            this.tabsName = arr[curLocal]
        })

        this.screenWidth = document.documentElement.clientWidth
        this.screenHeight = document.documentElement.clientHeight
        window.addEventListener('resize', () => {
            this.screenWidth = document.documentElement.clientWidth - this.topHeight
            this.screenHeight = document.documentElement.clientHeight - this.topHeight
            $idsTop = arr.map(item => {
                const itemTop = document.getElementById(item).offsetTop
                return itemTop
            })
        })
    },
    created () {  this.id = this.$route.query.billid

        this.getBasicInfo()
    },
    data () {
        return {
            tabsName: 'baseInfo',
            screenWidth: 0,
            screenHeight: 0,
            lastConHeight: 0,
            clickTabFlag: false, // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            basicForm: {
                'apportionedAmount': 0,
                'apportionedDate': '',
                'billId': '',
                'billNo': '',
                'businessTypeKey': '',
                'businessTypeValue': '',
                'deductionAmount': 0,
                'inputId': '',
                'inputName': '',
                'nullifyCreated': '',
                'nullifyCreator': '',
                'nullifyCreatorId': '',
                'nullifyDescription': '',
                'nullifyReason': '',
                'remarks': '',
                'state': 0,
                'subcontractorId': '',
                'subcontractorName': '',
                'workId': ''
            },
            query: {
                limit: 10,
                number: '',
                orgname: '',
                page: 1,
                states: [],
            },
            formLabelWidth: '150px',
            detailedTableData: [], //物资明细
            changeTableData: [], //变更信息
            basicInfo: {},
            stateArr: ['草稿', '审核中', '已审核', '已作废'],
            MouseEnterId: 0

        }
    },
    methods: {
        onChangeTab (e) {
            $.scrollTo(`#${e.name}`, 500)
            // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            this.clickTabFlag = true
            // 动画结束后，恢复状态
            setTimeout(() => {
                this.clickTabFlag = false
            }, 600)
        },
        // 获取最后一个内容区域的高度，计算底部空白
        getLastConHeight () {
            let si = setInterval(() => {
                // 因为dom异步加载，通过轮询找到渲染后的dom，获取高度
                if (document.getElementById('recodesCon')) {
                    const lastConHeight = document.getElementById('recodesCon').offsetHeight
                    console.log('lastConHeight: ', lastConHeight)
                    this.lastConHeight = lastConHeight
                    clearInterval(si)
                    si = null
                }
            }, 100)
        },

        getBasicInfo () {
            this.$Ajax
                .httpGet({
                    url: '/turnover/apportionedCost/findById',
                    params: {
                        billId: this.id
                    },
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                })
                .then(res => {
                    let arr = this.basicForm.dtls
                    for (let key in res) {
                        this.basicForm[key] = res[key]
                        this.basicForm.dtls = arr
                    }
                })
        },

        onSave () {
            // this.$Ajax
            //     .httpPost({
            //         url: '/turnover/apportionedCost/update',
            //         params: this.basicForm
            //     })
            //     .then(() => {
            //         this.clientPop('suc', '修改成功', this.getBasicInfo)

            //     })
        },
        //点击删除
        clickDel () {
            // this.clientPop(
            //     'info',
            //     '确定要删除吗？删除的数据可以在【回收站】还原或彻底删除。',
            //     this.delData
            // )
        },
        delData () {
            this.$Ajax
                .httpGet({
                    url: '/turnover/apportionedCost/delete',
                    params: { billId: this.id },
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                })
                .then(() => {
                    this.clientPop('suc', '删除成功', this.handleClose)
                })
        },

        handleClose () {
            this.$router.push('/revolveApportionedCostList')

        },

    }
}
</script>

<style lang="scss" scoped>
.e-table {
    min-height: auto;
    background: #fff;
}
.upload {
    margin: 20px auto;
    display: flex;
    justify-content: center;
    text-align: center;
}

.upload-demo {
    display: flex;
    justify-content: center;
    align-items: center;
}

::v-deep.el-input--suffix .el-input__inner {
    padding-right: 5px;
}
::v-deep.el-table .cell {
    text-align: center;
}
#iconEdit:hover {
  color: red;
}

.like {
  color: rgb(46, 97, 215);
  font-size: 17px;
  line-height: 30px;
  cursor: pointer;
}
</style>
